package com.hurl.waguji.biz.download;

import java.util.ArrayList;
import java.util.List;

import com.hurl.waguji.base.dao.HibernateDao;

class BOSaver {
	List<Object> bolist = new ArrayList<Object>();
	private int count = 0;
	private DownloadContext context;
	public BOSaver(DownloadContext context){
		this.context=context;
	}
	
	public void addBO(Object bo){
		count++;
		bolist.add(bo);
		if(count%20==0){
			saveOrUpdateBOList(bolist);
		}
	}
	public void finished(){
		saveOrUpdateBOList(bolist);
	}
	public int getTotalCount(){
		return count;
	}
	private void saveOrUpdateBOList(List<Object> bolist){
		HibernateDao dao = context.getDao();
		if(DownloadContext.BO_SAVE_MODE_UPDATE.equals(context.getBoSaveMode())){
			dao.saveOrUpdateList(bolist);
		}else{
			dao.saveList(bolist);
		}
		dao.flush();
		dao.clear();
		bolist.clear();
	}
}
